﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using App.Utilities.Web.Handlers;
using CRMBiz;
using CRMWeb.Utilities.Context;
using Newtonsoft.Json;

namespace CRMWeb.WebServices
{
    /// <summary>
    /// Summary description for DefaultCompany
    /// </summary>
    public class DefaultCompany : BaseHandler
    {
        public override void OnMethodInvoke(OnMethodInvokeArgs e)
        {
            context.Response.Cache.SetCacheability(HttpCacheability.ServerAndNoCache);
            context.Response.Cache.SetExpires(DateTime.MinValue);
            context.Response.Cache.SetNoStore();
            //e.Cancel = true;
            SetResponseContentType(ResponseContentTypes.HTML);
        }

        public override void AfterMethodInvoke(object result)
        {
            base.AfterMethodInvoke(result);
        }

       

        //Get default company setting data
        public void GetDefaultSetting()
        {
            try
            {
                var pageIndex = string.IsNullOrEmpty(context.Request["page"]) ? 1 : int.Parse(context.Request["page"]);
                var pageSize = string.IsNullOrEmpty(context.Request["rows"]) ? 10 : int.Parse(context.Request["rows"]);
                DataSet ds = DefaultCompanyHandler.GetDefaultSetting(pageIndex, pageSize);
                var total = (ds.Tables[0].Rows.Count > 0) ? Convert.ToInt32(ds.Tables[0].Rows[0]["Total"]) : 0;
                string strJson = JsonConvert.SerializeObject(ds.Tables[0]);
                strJson = "{\"total\":\"" + total.ToString() + "\",\"rows\":" + strJson + "}";

                context.Response.Write(strJson);
            }
            catch (Exception ex)
            {
                ErrorHandler.saveErrorLog(ex, CMSContext.CurrentUser.MemberName);
            }
        }

        
        //Bind all user names 
        public void BindDefaultUsers()
        {
            try
            {
                string strJson = JsonConvert.SerializeObject(DefaultCompanyHandler.BindDefaultUsers());
                context.Response.Write(strJson);
            }
            catch (Exception)
            {
                throw;
            }
        }

        //Bind all companies for users
        public void BindAllDefCompany()
        {
            try
            {
                string strJson = JsonConvert.SerializeObject(CmsMemberGroupHandler.GetAll());
                context.Response.Write(strJson);
            }
            catch (Exception)
            {
                throw;
            }
        }

        //Add new default company account setting
        public int AddDefaultCompany(int UserID, int UserDefaultgroupID)
        {
            int intReturnValue = 0;
            try
            {
                intReturnValue = DefaultCompanyHandler.AddDefaultCompany(UserID, UserDefaultgroupID);
            }
            catch (Exception ex)
            {
                ErrorHandler.saveErrorLog(ex, CMSContext.CurrentUser.MemberName);
            }
            return intReturnValue;
        }

        //Edit existing default company account setting
        public int EditDefaultCompany(int UsercompanyID, int UserID, int UserDefaultgroupID)
        {
            int intReturnValue = 0;
            try
            {
                intReturnValue = DefaultCompanyHandler.EditDefaultCompany(UsercompanyID, UserID, UserDefaultgroupID);
            }
            catch (Exception ex)
            {
                ErrorHandler.saveErrorLog(ex, CMSContext.CurrentUser.MemberName);
            }
            return intReturnValue;
        }

        //Delete default company account setting
        public bool DeleteDefaultCompany(int UsercompanyID)
        {
            bool flag = false;
            try
            {
                flag = DefaultCompanyHandler.DeleteDefaultCompany(UsercompanyID);
            }
            catch (Exception ex)
            {
                ErrorHandler.saveErrorLog(ex, CMSContext.CurrentUser.MemberName);
            }
            return flag;
        }

    }
}